<?php

class RequirementController extends Zend_Controller_Action
{
	private $userType;
	private $user_id;
	
    public function init()
    {
        $authData = Zend_Auth::getInstance()->getStorage()->read();
		if (is_array($authData) and $authData['identity'] != 'anon'){
		    if (isset($authData['userType']) and strtolower($authData['userType']) == 'agnt') {
		        $this->userType = strtolower($authData['userType']);
		        $this->user_id = strtolower($authData['identity']);
		    } 
        }
    }

    public function indexAction()
    {
        if($this->userType){
        	$this->view->assign('userType',$this->userType);
        }
    }
    
    public function postAction(){
    	$this->_helper->viewRenderer->setNoRender(TRUE);
        $this->_helper->layout()->disableLayout();
        $request = $this->getRequest();
        $params = array_diff($request->getParams(), $request->getUserParams());
        $req_param = array();
        foreach ($params as $colName => $value) {
            $value = is_array($value)?$value:htmlentities(trim($value));
            $req_param[$colName] = $value;
        }
        $oper = strtolower($params['oper']);
		switch ($oper) {
            case 'add':
            	$req = new Reale_Model_Requirement();
            	$req->setCategory_id($req_param['category_id'])
            		->setLooking_for($req_param['looking_for'])
            		->setEnquiry($req_param['enquiry']);
            	if($this->userType!='agnt'){
            		$req->setName($req_param['name'])
            			->setContact_no($req_param['contact_no'])
            			->setEmail($req_param['email']);
            	}
            	else{
            		$req->setAgent_id($this->user_id);
            	}
				if ($req->save()) {
                    echo ' Posted Successfully with id: '.$req->getRequirement_id() ;
                    return;
                }
                else{
                	throw new Exception('Cannot post requirement successfully.', Zend_Log::ERR);
                }
            	break;
            default:
            	throw new Zend_Exception('"' . $oper . '" is unknown operation.', 
                Zend_Log::ERR);
                break;	
		}
		throw new Zend_Exception('Operation is not successfull.',Zend_Log::ERR);
        
        
    	
    }

}

